package org.broadway.monitor;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.broadway.interfaces.Context;

/**
 * This class implements a simple context.
 * A context object is used to pass data between broadway components.
 * The context uses an internal map to store objects as they are passed around.
 * 
 * @author vmatters
 *
 */
public class SimpleContext implements Serializable, Context{
	private static Log log = LogFactory.getLog(SimpleContext.class);
	
	private Map<Object,Object> values;
	
	/**
	 * Constructor
	 *
	 */
	public SimpleContext() {
		values = new HashMap<Object,Object>();
	}
	
	/**
	 * A constructor that accepts an existing map with values.
	 * @param values
	 */
	public SimpleContext(Map<Object,Object> values) {
		this.setValues(values);
	}	
	
	/**
	 * Returns value of object identified by key.
	 * @param key - key for retrieved object
	 * @return object retrieved
	 */
	public Object getValue(Object key) {
		log.debug("Retrieved value with key " + key + " from context.");
		return values.get(key);
	}

	/**
	 * Stores a value in the context.
	 * @param key for value to store
	 * @param value being stored
	 */
	public void putValue(Object key, Object value) {
		values.put(key, value);
		log.debug("Value with key " + key + " added to context.");
	}
	
	/**
	 * Returns internal map for all values in context.
	 * @return
	 */
	public Map getValues() {
		return values;
	}
	
	/**
	 * Sets the internal context values with provided map.
	 * @param values
	 */
	public void setValues(Map<Object,Object> values){
		this.values = values;
	}
	
}
